/**
 * @Title: DeptServiceImpl.java
 * @Package com.kaver.sso.service.impl
 * @author roc
 * @date Aug 9, 2021 10:49:44 AM
 * @version V1.0
 * @copyright kaver
 */
package com.kaver.sso.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kaver.sso.dao.po.SsoDept;
import com.kaver.sso.dao.service.SsoDeptDao;
import com.kaver.sso.dao.service.SsoUserDao;
import com.kaver.sso.def.BizException;
import com.kaver.sso.def.dto.DeptDto;
import com.kaver.sso.def.enums.CodeEnum;
import com.kaver.sso.def.req.AddDeptReq;
import com.kaver.sso.def.req.DelDeptReq;
import com.kaver.sso.def.req.DeptListReq;
import com.kaver.sso.def.req.DeptPageListReq;
import com.kaver.sso.def.req.UpdateDeptReq;
import com.kaver.sso.def.resp.PageResp;
import com.kaver.sso.service.DeptService;

/**
 * @ClassName: DeptServiceImpl
 * @Description: 部门
 * @author roc
 * @date Aug 9, 2021 10:49:44 AM
 * 
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    SsoDeptDao ssoDeptDao;
    @Autowired
    SsoUserDao ssoUserDao;
    
    @Override
    public void addDept(AddDeptReq req) {
        SsoDept entity = new SsoDept();
        entity.setDeptName(req.getDeptName());
        ssoDeptDao.save(entity);
    }

    @Override
    public void delDept(DelDeptReq req) {
        SsoDept dept = ssoDeptDao.getById(req.getDeptId());
        if (dept == null) {
            throw new BizException(CodeEnum.F_000010);
        }
        int count = ssoUserDao.queryDeptUserCount(dept.getId());
        if (count > 0) {
        	throw new BizException(CodeEnum.F_000001.getCode(), "当前部门存在用户，请先清除用户");
        }
        dept.deleteById();
    }

    @Override
    public void updateDept(UpdateDeptReq req) {
        SsoDept dept = ssoDeptDao.getById(req.getDeptId());
        if (dept == null) {
            throw new BizException(CodeEnum.F_000010);
        }
        dept.setDeptName(req.getDeptName());
        dept.updateById();
    }

    @Override
    public PageResp<DeptDto> deptPageList(DeptPageListReq req) {
        QueryWrapper<SsoDept> queryWrapper = new QueryWrapper<SsoDept>();
        if (req.getDeptName() != null) {
            queryWrapper.like("dept_name", req.getDeptName());
        }
        queryWrapper.orderByDesc("id");
        IPage<SsoDept> page = new Page<>(req.getPage(), req.getSize());
        IPage<SsoDept> iPage = ssoDeptDao.page(page, queryWrapper);
        PageResp<DeptDto> pageResp = new PageResp<>();
        List<DeptDto> lists = new ArrayList<>();
        List<SsoDept> depts = iPage.getRecords();
        if (!CollectionUtils.isEmpty(depts)) {
            for (SsoDept dept : depts) {
                DeptDto dto = new DeptDto();
                dto.setDeptId(dept.getId());
                dto.setDeptName(dept.getDeptName());
                lists.add(dto);
            }
        }
        pageResp.setLists(lists);
        pageResp.setTotal(iPage.getTotal());
        return pageResp;
    }

    @Override
    public List<DeptDto> deptList(DeptListReq req) {
        QueryWrapper<SsoDept> queryWrapper = new QueryWrapper<SsoDept>();
        if (req.getDeptName() != null) {
            queryWrapper.like("dept_name", req.getDeptName());
        }
        queryWrapper.orderByDesc("id");
        List<SsoDept> depts = ssoDeptDao.list(queryWrapper);
        List<DeptDto> lists = new ArrayList<>();
        if (!CollectionUtils.isEmpty(depts)) {
            for (SsoDept dept : depts) {
                DeptDto dto = new DeptDto();
                dto.setDeptId(dept.getId());
                dto.setDeptName(dept.getDeptName());
                lists.add(dto);
            }
        }
        return lists;
    }

}
